package com.example.wzdjcrdpassword.controller;

import com.example.wzdjcrdpassword.cache.SystemCacheManager;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 缓存管理控制器
 * 提供缓存状态查看和刷新功能
 */
@RestController
@RequestMapping("/api/cache")
@RequiredArgsConstructor
public class CacheController {
    
    private final SystemCacheManager systemCacheManager;
    
    /**
     * 获取缓存统计信息
     */
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getCacheStats() {
        Map<String, Object> stats = systemCacheManager.getCacheStats();
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 刷新系统缓存
     */
    @PostMapping("/refresh")
    public ResponseEntity<Map<String, Object>> refreshCache() {
        systemCacheManager.refreshCache();
        Map<String, Object> stats = systemCacheManager.getCacheStats();
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 清空系统缓存
     */
    @PostMapping("/clear")
    public ResponseEntity<Map<String, Object>> clearCache() {
        systemCacheManager.refreshCache(); // refreshCache会清空并重新加载
        Map<String, Object> stats = systemCacheManager.getCacheStats();
        return ResponseEntity.ok(stats);
    }
}